package service

import (
	"encoding/json"
	"io"
	"log"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/lzppppppp/virapaksa-go/internal/system_monitor/middleware"
	"github.com/lzppppppp/virapaksa-go/internal/system_monitor/model"
	"github.com/spf13/viper"
)

// 获取批次信息
func GetBatch(context *gin.Context) {
	urlData := model.FuncVar{
		IP:   viper.GetString("es.host"),
		Port: viper.GetString("es.port"),
		API:  viper.GetString("es.index.menu") + "/_doc/batch-info/_source",
	}
	response := middleware.GetData(urlData)

	if response.StatusCode != 200 {
		panic("Failed to get batch-info from ES")
	} else {
		log.Printf("INFO: Get batch-info success.\n")
	}
	body, _ := io.ReadAll(response.Body)

	var data map[string]interface{}

	// 解析http返回的json体
	if err := json.Unmarshal(body, &data); err == nil {
		log.Printf("ERROR: Data parsing failed, data is %v.\n", data["value"])
	}

	context.JSON(http.StatusOK, data)
}
